package yang.yu.sunny.domain;

import org.dayatang.domain.EntityRepository;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import static java.util.stream.Collectors.toMap;

/**
 * Created by yyang on 16/3/29.
 */
public class ProductCategories {

    private EntityRepository repository;

    public ProductCategories(EntityRepository repository) {
        this.repository = repository;
    }

    public Optional<ProductCategory> get(Long id) {
        return Optional.ofNullable(repository.get(ProductCategory.class, id));
    }

    public Optional<String> name(Long id) {
        return get(id).map(ProductCategory::getName);
    }

    public Map<Long, ProductCategory> of(Set<Long> ids) {
        List<ProductCategory> results = repository.createCriteriaQuery(ProductCategory.class)
                .in("id", ids).list();
        return results.stream().collect(toMap(ProductCategory::getId, Function.identity()));
    }

    public Map<Long, String> names(Set<Long> ids) {
        List<ProductCategory> results = repository.createCriteriaQuery(ProductCategory.class)
                .in("id", ids).list();
        return results.stream().collect(toMap(ProductCategory::getId, ProductCategory::getName));
    }
}
